package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyExtensionAdapter;
import com.yiwei.jmx.adapter.TabTopAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.SpreadListsBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.view.OnLoadeMoreListener;
import com.yiwei.jmx.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class MyExtensionActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.tablayout)
    RecyclerView tablayout;
    @BindView(R.id.btn_one)
    SuperButton btnOne;
    @BindView(R.id.btn_two)
    SuperButton btnTwo;
    @BindView(R.id.btn_three)
    SuperButton btnThree;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.btn_apply)
    SuperButton btnApply;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbinder;
    private TabTopAdapter TabAdapter;
    private MyExtensionAdapter adapter;
    private int[][] select_res = {{0xffE7F2FF, 0xff0072FF}, {0xffF6F6F6, 0xff000000}};
    private String status = "1";
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getMySpreadLists(page);
        }
    };
    private boolean is_shenhe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_extension);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {

        txtTitle.setText("我的推广");
        is_shenhe = getIntent().getBooleanExtra("is_shenhe", false);


        TabAdapter = new TabTopAdapter(this);
        TabAdapter.setOnChooseListener(new TabTopAdapter.OnChooseListener() {
            @Override
            public void onClick(int poi) {


            }
        });
        tablayout.setLayoutManager(new GridLayoutManager(this, 3));
        tablayout.setAdapter(TabAdapter);
        List<String> tab_titles = new ArrayList<>();
        tab_titles.add("审核成功");
        tab_titles.add("审核中");
        tab_titles.add("审核失败");
        TabAdapter.update(tab_titles);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getMySpreadLists(1);
            }
        });
        adapter = new MyExtensionAdapter(this);
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

        if (is_shenhe) {
            status = "0";
            adapter.setStatus(status);
            TabAdapter.setSelectPoi(1);
        }


        getMySpreadLists(1);
    }

    public void getMySpreadLists(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        SpreadListsBean bean = new SpreadListsBean();
        bean.page = page;
        bean.status = status;

        Api.api_service.getMySpreadLists(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.data.lists, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                        if (page == 1) {
                            list.smoothScrollToPosition(0);
                        }
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    private void selectMenu(int poi) {
        btnOne.setShapeStrokeWidth(0).setUseShape();
        btnTwo.setShapeStrokeWidth(0).setUseShape();;
        btnThree.setShapeStrokeWidth(0).setUseShape();;


        if (poi == 0) {
            btnOne.setShapeSolidColor(select_res[0][0]).setShapeStrokeColor(select_res[0][1]).setShapeStrokeWidth(1).setUseShape();
            btnOne.setTextColor(select_res[0][1]);

            btnTwo.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnTwo.setTextColor(select_res[1][1]);

            btnThree.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnThree.setTextColor(select_res[1][1]);

            status = "1";
        } else if (poi == 1) {
            btnTwo.setShapeSolidColor(select_res[0][0]).setShapeStrokeColor(select_res[0][1]).setShapeStrokeWidth(1).setUseShape();
            btnTwo.setTextColor(select_res[0][1]);

            btnOne.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnOne.setTextColor(select_res[1][1]);

            btnThree.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnThree.setTextColor(select_res[1][1]);
            status = "0";
        } else if (poi == 2) {
            btnThree.setShapeSolidColor(select_res[0][0]).setShapeStrokeColor(select_res[0][1]).setShapeStrokeWidth(1).setUseShape();
            btnThree.setTextColor(select_res[0][1]);

            btnTwo.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnTwo.setTextColor(select_res[1][1]);

            btnOne.setShapeSolidColor(select_res[1][0]).setUseShape();
            btnOne.setTextColor(select_res[1][1]);
            status = "2";
        }
        adapter.setStatus(status);
        onLoadeMoreListener.setPage();
        getMySpreadLists(1);
    }

    @OnClick({R.id.btn_one, R.id.btn_two, R.id.btn_three})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_one:
                selectMenu(0);
                break;
            case R.id.btn_two:
                selectMenu(1);
                break;
            case R.id.btn_three:
                selectMenu(2);
                break;
        }
    }
}
